<?php
namespace app\admin\controller;
use app\admin\model\brand;
use app\admin\model\goods;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use think\Filesystem;

class  Ppgl extends BaseController
{
    public function pplb(){
        //1接参，过滤
        $data = Request::get('','','trim,htmlspecialchars,strip_tags');

        //2调用模型类获取数据
        $list = \app\admin\model\brand::getPinList($data,$data);
        //3模板赋值
        $empty = "<tr><td colspan='8'>暂无数据！</td></tr>";
        View::assign('empty',$empty);
        View::assign($data);
        //4模板渲染
        return View::fetch('',['list'=>$list]);
    }
//        public function pplb1(){
//            $brand_name=Request::get('brand_name','','trim,htmlspecialchars,strip_tags');
//            $recommended=Request::get('recommended','2','trim,htmlspecialchars,strip_tags');
//            $tu=Request::get('tu',[],'trim,htmlspecialchars,strip_tags');
//            $tu=implode(',',$tu);
//            $db=Db::table('ecm_brand')->field(true);
//            if (''!==$brand_name){
//                $db->where('brand_name','like','%'.$brand_name.'%');
//            }
//            View::assign('brand_name',$brand_name);
//            if ('2'!==$recommended){
//                $db->where('recommended',$recommended);
//            }
//            View::assign('recommended',$recommended);
//
//        if (""!==$tu){
//            if ($tu==="1"){
//                $db->whereNotNull('brand_logo');
//                $db->where('brand_logo','<>',"");
//            }else if ($tu==="0"){
//                $db->where(function ($q){
//                    $q->whereNull('brand_logo');
//                    $q->whereOr('brand_logo','=',"");
//                });
//
//            }
//        }
//
//        $tu = explode(",",$tu);
//        View::assign('tu',$tu);
//        $arr=[
//            'list_rows'=>5,
//            'query'=>Request::instance()->param(),
//        ];
//        $pplb=$db->paginate($arr);
//        View::assign('pplb',$pplb);
//        $empty = "<tr><td colspan='11'>暂无数据！</td></tr>";
//        View::assign('empty',$empty);
//        return View::fetch();
//    }
    public function pplbadd()
    {
        if (Request::isPost()){
            $postParam = Request::post('','','trim,htmlspecialchars,strip_tags');
            try{
                $brand_logo = Request::file('brand_logo');

                $rule = ['brand_logo'=>'filesize:102040|fileExt:jpg|image:500,500,jpg'];
                $data = ['brand_logo'=>$brand_logo];
                validate($rule)->check($data);

                //验证通过 执行上传
                //$savename = \think\facade\Filesystem::putFile('upload',$brand_logo);
                //$postParam['brand_logo'] = str_replace('\\','/',$savename);
                //首先上传到临时文件夹，然后等保存数据库成功在移走
                $savename = \think\facade\Filesystem::putFile('temp',$brand_logo,'get_uploadfile_name');
                //获取上传文件的路径
                $savename = str_replace('\\','/',$savename);
                //通过临时文件路径构建正式路径，并将正式路径存入数据库（等待保存成功后移动文件）
                list($begin_dir,$filename) = explode('/',$savename,2);
                $newname = 'upload/' . $filename;
                $postParam['brand_logo'] = $newname;


            }catch (\think\exception\ValidateException $e){
                $this->error($e->getError());
        }catch (\Exception $e){}
        try{
             validate(\app\admin\validate\brand::class)->check($postParam);
        }catch (\think\exception\ValidateException $e){
                $this->error($e->getMessage());
        }
            if (isset($postParam['qwert'])){
                $url = 'pplbadd';
                unset($postParam['qwert']);
            }
            if (isset($postParam['qwer'])){
                $url = 'pplb';
                unset($postParam['qwer']);
            }
            $Brandadd= new \app\admin\model\brand();
            $db = $Brandadd->add($postParam);
            if ($db){
                //移动文件
                //复制临时文件到正式目录下
                //注意如果拷贝到的文件夹不存在，需要创建一下
                $arr = explode('/',$newname);
                unset($arr[count($arr)-1]);
                $dir = implode('/',$arr);
                $dir = 'storage/'.$dir;
                if (!is_dir($dir)){
                    mkdir($dir,0777,true);
                }
                copy('storage/'.$savename,'storage/'.$newname);
                //删除原临时文件
                unlink('storage/'.$savename);

                $this->success("成功",url("{$url}"));
            }else{
                $this->error("失败！原因：".$Brandadd->getError());
            }
        }else{
            return View::fetch();
        }

    }
    public function pplbedit(){
        if (Request::isPost()){
            $post = Request::post('','','trim,htmlspecialchars,strip_tags');
            $get = Request::get('','','trim,htmlspecialchars,strip_tags');

            if (isset($get['id']))      unset($get['id']);

            $brandObj = new \app\admin\model\brand();
            $result = $brandObj->upt($post);
            try{
                validate(\app\admin\validate\brand::class)->scene('edit')->check($post);
            }catch (\think\exception\ValidateException $e){
                $this->error($e->getMessage());
            }
            if ($result){

                $this->success('修改成功',url('pplb',$get));
            }else{

                $this->error('修改失败');
            }
        }else{
            $get = Request::get('','','trim,htmlspecialchars,strip_tags');
            $id = $get['id']??'';
            $brandObj = Brand::find($id);

            if (empty($brandObj)){

                $this->error('参数错误',url('pplb'));
            }

            return View::fetch('',$brandObj->toArray());
        }
    }
    public function del1(){
        $id = Request::get('id','','trim,htmlspecialchars,strip_tags');
        if (is_string($id) && false !== strpos($id,',')){
            $id = explode(',',$id);
        }

        $result = brand::delbrand($id);
        if ($result){
            $this->success("删除成功");
        }else{
            $this->error("删除失败！品牌被占用");
        }
    }
    public function del()
    {
        $del = Request::get("del", '', 'trim,htmlspecialchars,strip_tags');
        if ('' !== $del) {
            $rt = brand::destroy(function ($query) use ($del) {
                $query->where('brand_id', 'in', $del);
            });
            if ($rt == 1) {
                $this->success("删除成功", url('pplb'));
            } else {
                $this->error('删除失败');
            }
        }
    }

}